package com.example.hello.client.Modal;

import com.example.hello.BootstrapModal;
import com.github.gwtbootstrap.client.ui.constants.BackdropType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapModal.class)
public class BootstrapModalConnector extends AbstractComponentConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapModal.class);
    }

    @Override
    public VBootstrapModal getWidget() {
        return (VBootstrapModal) super.getWidget();
    }

    @Override
    public BootstrapModalState getState() {
        return (BootstrapModalState) super.getState();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapModalState state = getState();

        VBootstrapModal modal = getWidget();

        for (Connector connector : state.getWidgets()) {
            modal.add(((ComponentConnector) connector).getWidget());
        }
        modal.setAnimation(state.isAnimation());
        modal.setBackdrop(BackdropType.valueOf(state.getBackdrop()));
        modal.setCloseVisible(state.isCloseVisible());
        modal.setDynamicSafe(state.isDynamicSafe());
        modal.setKeyboard(state.isKeyboard());

        if (state.isShowFlag()) {
            System.out.println("true");
            modal.show();
        } else {
            System.out.println("false");
            modal.hide();
        }
    }

}
